local skel = fk.CreateSkill {
  name = "efengqi__zhengu",
}

Fk:loadTranslationTable{
  ["efengqi__zhengu"] = "镇骨",
  [":efengqi__zhengu"] = "结束阶段，你可以选择两次令一名角色："..
  "<br>①摸两张牌并受到你的1点冰冻伤害；"..
  "<br>②将手牌调整至与你相同。"..
  "<br>若第二次选择同一项，延迟至其下个回合结束执行。",

  ["#efengqi__zhengu-choose"] = "镇骨：选择一名角色，对其造成冰伤害，或令其调整手牌",
  ["#efengqi__zhengu-choice"] = "镇骨：对 %src 执行一项（第 %arg 项，共 2 项）",
  ["#efengqi__zhengu_delay"] = "镇骨",
  ["@@efengqi__zhengu"] = "镇骨",
  ["efengqi__zhengu_damage"] = "令其摸2牌并受到1点冰伤",
  ["efengqi__zhengu_adjust"] = "令其将手牌调整至与你相同",

  ["$efengqi__zhengu1"] = "镇守城池，必以骨相拼！",
  ["$efengqi__zhengu2"] = "孔明计虽百算，却难敌吾镇骨千具！",
}

---@param victim ServerPlayer
---@param source ServerPlayer
local doZhengu = function (victim, source, choice)
  if victim.dead then return end
  local room = victim.room
  if choice == "efengqi__zhengu_damage" then
    victim:drawCards(2, skel.name)
    if not victim.dead then
      room:doIndicate(source, {victim})
      room:damage { from = source, to = victim, damage = 1, skillName = skel.name, damageType = fk.IceDamage }
    end
  else
    local x = victim:getHandcardNum() - source:getHandcardNum()
    if x > 0 then
      room:askToDiscard(victim, {min_num = x, max_num = x, include_equip = false, skill_name = skel.name, cancelable = false})
    elseif x < 0 then
      victim:drawCards(-x, skel.name)
    end
  end
end

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player.phase == Player.Finish
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local tos = room:askToChoosePlayers(player, { targets = room.alive_players, min_num = 1, max_num = 1,
      prompt = "#efengqi__zhengu-choose", skill_name = skel.name })
    if #tos > 0 then
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local chosen = {}
    for i = 1, 2 do
      local choice = room:askToChoice(player, { choices = {"efengqi__zhengu_damage", "efengqi__zhengu_adjust"},
       skill_name = skel.name, prompt = "#efengqi__zhengu-choice:"..to.id.."::"..i})
      table.insert(chosen, choice)
    end
    if chosen[1] ~= chosen[2] then
      for _, ch in ipairs(chosen) do
        doZhengu(to, player, ch)
      end
    else
      room:addTableMark(to, "@@efengqi__zhengu", {player.id, chosen[2]})
      doZhengu(to, player, chosen[1])
    end
  end,
})

skel:addEffect(fk.TurnEnd, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("@@efengqi__zhengu") ~= 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local invoked = {}
    local mark = player:getMark("@@efengqi__zhengu")
    room:setPlayerMark(player, "@@efengqi__zhengu", 0)
    for _, info in ipairs(mark) do
      if player.dead then return end
      local from = room:getPlayerById(info[1])
      local choice = info[2]
      if not from.dead then
        if table.insertIfNeed(invoked, from.id) then
          room:notifySkillInvoked(from, skel.name, "control", {player.id})
          player:broadcastSkillInvoke(skel.name)
          room:doIndicate(from, {player})
        end
        doZhengu(player, from, choice)
      end
    end
  end,
})

return skel
